package com.vast.ui.component.socket;

import com.vast.ui.component.socket.listener.IReceiveListener;
import com.vast.ui.component.socket.listener.ISocketConnectionListener;
import com.vast.ui.component.socket.models.RequestQueue;
import com.vast.ui.component.socket.observers.DecodeRequester;
import com.vast.ui.component.socket.state.Sequence;

public class ServerServiceController implements ISocketServer {
	public  int                  serverRecvPort;
	public  int                  serverSendPort;
	public  boolean              receiveServerEnabled = true;
	public  boolean              sendServerEnabled = false;
	private ISocketService       receiveService = null;
	private ISocketService       sendService    = null;
	private DecodeRequester      decodeRequester = null;
	private RequestQueue         receiveQueue     = null;
	private final Sequence       sequence;
	
	public ServerServiceController(String pluginId, int serverPort, Sequence sequence, RequestQueue sendQueue, int receivedWait, boolean receiveServerEnabled, boolean sendServerEnabled) {
		this.sequence         = sequence;
		this.receiveServerEnabled = receiveServerEnabled;
		this.sendServerEnabled    = sendServerEnabled;
		this.serverRecvPort   = serverPort;
		this.serverSendPort   = serverPort + 1;
		
		if(this.receiveServerEnabled) {
			this.receiveQueue     = new RequestQueue();
			this.decodeRequester  = new DecodeRequester(pluginId, sequence);
			this.receiveService   = new ReceiveService(pluginId, sequence, receiveQueue, decodeRequester, receivedWait);
		}
		
		if(this.sendServerEnabled) {
			this.sendService      = new SendService(pluginId, sequence, sendQueue);
		}
	}
	
	public void openConnection() {
		if(!this.sequence.isReceiveConnection() && this.receiveServerEnabled) {
			this.receiveService.startup(serverRecvPort);
			this.sequence.openReceiveConnection();
		}
		if(!this.sequence.isSendConnection() && this.sendServerEnabled) {
			this.sendService.startup(serverSendPort);
			this.sequence.openSendConnection();
		}
	}

	public void closeConnection() {
		// TODO Auto-generated method stub
		if(this.receiveServerEnabled) {
			this.sequence.closeReceiveConnection();
			this.receiveService.shutdown();
		}
		
		if(this.sendServerEnabled) {
			this.sequence.closeSendConnection();
			this.sendService.shutdown();
		}
	}

	public void addReceiveListener(IReceiveListener listener) {
		// TODO Auto-generated method stub
		if(!this.receiveServerEnabled) {
			return;
		 }
		this.decodeRequester.addReceiveListener(listener);
	}

	public void addSocketConnectionListener(ISocketConnectionListener listener) {
		// TODO Auto-generated method stub
		if(!this.receiveServerEnabled) {
			return;
		}
		this.decodeRequester.addSocketConnectionListener(listener);
	}

	public void removeReceiveListener(IReceiveListener listener) {
		// TODO Auto-generated method stub
		if(!this.receiveServerEnabled) {
			return;
		}
		this.decodeRequester.removeReceiveListener(listener);
	}

	public void removeSocketConnectionListener(
			ISocketConnectionListener listener) {
		// TODO Auto-generated method stub
		if(!this.receiveServerEnabled) {
			return;
		}
		this.decodeRequester.removeSocketConnectionListener(listener);
	}
}
